import { Module } from 'vuex';
import { State } from '..';
import { Todo } from '../../types';

const initialState = {
    items: [] as Todo[]
}
export type TodoState = typeof initialState

export default {
    namespaced: true,
    state: initialState,
    mutations: {
        initTodo(state, payload: Todo[]) {
            state.items = payload
        },
        addTodo(state, payload: Todo) {
            state.items.push(payload)
        }
    },
    actions: {
        initTodo({ commit }) {
            setTimeout(() => {
                commit('initTodo', [
                    {
                        id: 1,
                        name: '王棋',
                    } as Todo
                ])
            }, 500);
        },
        addTodo({ commit, state }, payload: string) {
            commit('addTodo', {
                id: state.items.length + 1,
                name: payload
            } as Todo)
        }
    }



} as Module<TodoState, State>
